package com.fqgj.sentry.policy.engine.eval.script
/**
 * Created by kennykong on 11/07/2017.
 */

def weightScore(var, a1, a2) {
    if (var == null) var = 0d
    def tabR = a1 + a2 * var
    return processNum(tabR)
}

def calculateScore(var, a1, a2) {
    if (var == null) var = 0d
    def tabR = a1 + a2 * var
    return processNum(tabR)
}

/**
 * 保留2位小数Double
 * @param num
 * @return
 */
def processNum(num) {
    if (num == null) return 0d
    def rst = num.toBigDecimal()
    def rst1 = rst.setScale(2, BigDecimal.ROUND_HALF_UP)
    return rst1.toDouble()
}

def dateCompare(var, x) {
    return var.compareTo(x)
}

def gte(var, x) {
    if(var != null) {
        if(var.getClass().is(Date.class)) {
            def r = dateCompare(var, x)
            return r >=0
        }
    }
    return var >= x
}

def gt(var, x) {
    if(var != null) {
        if(var.getClass().is(Date.class)) {
            def r = dateCompare(var, x)
            return r >0
        }
    }
    return var > x
}

def lte(var, x) {
    if(var != null) {
        if(var.getClass().is(Date.class)) {
            def r = dateCompare(var, x)
            return r <=0
        }
    }
    return var <= x
}

def lt(var, x) {
    if(var != null) {
        if(var.getClass().is(Date.class)) {
            def r = dateCompare(var, x)
            return r <0
        }
    }
    return var < x
}

def eq(var, x) {
    if(var != null) {
        if(var.getClass().is(Date.class)) {
            def r = dateCompare(var, x)
            return r ==0
        }
    }
    return var == x
}

def contains(var, x) {
    if (var == null) return false
    return var.contains(x)
}

def notContains(var, x) {
    return !(contains(var, x))
}

def neq(var, x) {
    if(var != null) {
        if(var.getClass().is(Date.class)) {
            def r = dateCompare(var, x)
            return r !=0
        }
    }
    return var != x
}